package array.generateMatrix;

/**
 * @ClassName GenerateMatrix
 * @Description TODO
 * @Author lenovo
 * @Date 2023-02-14 11:10
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class GenerateMatrix {

    /**
     * 59. 螺旋矩阵 II
     * 给你一个正整数 n ，生成一个包含 1 到 n2 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
     * <p>
     * 输入：n = 3
     * 输出：[[1,2,3],[8,9,4],[7,6,5]]
     *
     * @return
     */

    public int[][] generateMatrix(int n) {
        int start = 1;
        int end = n * n;

        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = n - 1;
        int[][] res = new int[n][n];
        while (start <= end) {
            for (int i = left; i <= right; i++) {
                res[top][i] = start++;
            }
            top++;

            for (int i = top; i <= bottom; i++) {
                res[i][right] = start++;
            }
            right--;

            for (int i = right; i >= left; i--) {
                res[bottom][i] = start++;
            }
            bottom--;

            for (int i = bottom; i >= top; i--) {
                res[i][left] = start++;
            }
            left++;

        }
        return res;

    }


    public static void main(String[] args) {
        GenerateMatrix generateMatrix = new GenerateMatrix();


        int[][] ints = generateMatrix.generateMatrix(3);
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                System.out.println(ints[i][j]);
            }
        }


    }


//    class Solution {
//        public int[][] generateMatrix(int n) {
//
//
//        }
//    }
}